package cn.echogo.demo.location;

import java.util.List;

import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import cn.echogo.framework.location.EGItemizedOverlay;
import cn.echogo.framework.location.EGLocationListener;
import cn.echogo.framework.location.LocationUtil;
import cn.echogo.ipart.R;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class EGMapActivity extends MapActivity {
	
	MapView mapView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.demo_map);

		mapView = (MapView) findViewById(R.id.mapView);
		mapView.setBuiltInZoomControls(true);		//缩放模式
		mapView.setSatellite(false);		//是否使用卫星地图





		//直接获取位置
//		getLocationDirectly();
		
		//回调获取位置
		requestLocation();

	}
	
	private void requestLocation() {
		LocationListener listener = new EGLocationListener(this);
		LocationUtil.requestLocation(this, LocationManager.NETWORK_PROVIDER, listener);
	}
	
	private void getLocationDirectly() {
		
//		GeoPoint p = new GeoPoint((int) 40.748356 * 1000000, (int) -73.984621 * 1000000);
		MapController mc = mapView.getController();
		mc.setZoom(18);		//1-21
//		mc.animateTo(p);
		
		Location location = LocationUtil.getLocation(this);
		if (location != null) {
			GeoPoint point = LocationUtil.getGeoPointByLocation(location);
			System.out.println("latitude = " + point.getLatitudeE6() + ", longitude = " + point.getLongitudeE6());

			if (point != null) {
				Address mAddress = LocationUtil.getAddressByGeoPoint(this, point);
				System.out.println(mAddress.getCountryName() + ", " + mAddress.getAdminArea()  + ", " + mAddress.getLocality() + ", " + mAddress.getPremises() + ", " + mAddress.getAddressLine(0) );

				mc.animateTo(point);
				
				List<Overlay> overlays = mapView.getOverlays();
//				Overlay overlay = new EGOverlay(this, point);
//				overlays.add(overlay);
				
				Drawable drawble = getResources().getDrawable(R.drawable.gps_icon);
				EGItemizedOverlay itemizedOverlay = new EGItemizedOverlay(drawble, this);
				OverlayItem overlayItem = new OverlayItem(point, "Title", "snippet");
				itemizedOverlay.addOverlayItem(overlayItem);
				overlays.add(itemizedOverlay);
				
				
			}

		}
	}


	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

}
